/*
 *  $Id$
 *  Copyright [2010] [Panxiaobo] 
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  you may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 *  Unless required by applicable law or agreed to in writing, software 
 *  distributed under the License is distributed on an "AS IS" BASIS, 
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 *  See the License for the specific language governing permissions and 
 *  limitations under the License. 
 */
package pxb;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pxb.openid.Association;
import pxb.openid.ns.Ax;
import pxb.openid.rp.servlet.BaseOpenidServlet;

/**
 * @author Panxiaobo
 * 
 */
@SuppressWarnings("serial")
public class OpenidServlet extends BaseOpenidServlet {
    // private static final Logger log = LoggerFactory.getLogger(OpenidServlet.class);

    Map<String, String> serverCache = new HashMap<String, String>();
    Map<String, Association> associationCache = new HashMap<String, Association>();

    @Override
    protected String mapOpenidProvider(final String openidAlias) {
        if (openidAlias == null) {
            return null;
        }
        try {
            String x;
            if (openidAlias.startsWith("http://") || openidAlias.startsWith("https://")) {
                x = openidAlias;
            } else {
                x = "http://" + openidAlias;
            }
            URL url = new URL(x);
            String host = url.getHost();
            if (host.equals("google.com") || host.endsWith(".google.com")) {
                return "https://www.google.com/accounts/o8/id";
            }
        } catch (Exception ex) {

        }
        return openidAlias;
    }

    @Override
    protected String getOpenidServerCache(String openid) {
        return serverCache.get(openid);
    }

    @Override
    protected void putOpenidServerCache(String openid, String server) {
        serverCache.put(openid, server);
    }

    @Override
    protected Association getAssociationCache(String server) {
        return associationCache.get(server);
    }

    @Override
    protected void putAssociationCache(String server, Association association) {
        associationCache.put(server, association);
    }

    @Override
    protected Map<String, String> extraParams() {
        return new Ax().request().askForCountry(false).askForEmail(false).askForFirstName(false).askForLastName(false)
                .askForLanguage(false).getMap();
    }

    @Override
    protected void fail(HttpServletRequest req, HttpServletResponse resp, Map<String, String> values)
            throws ServletException, IOException {
        req.setAttribute("status", "fail");
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }

    @Override
    protected void success(HttpServletRequest req, HttpServletResponse resp, String openid,
            Map<String, String> trusted, Map<String, String> values) throws ServletException, IOException {
        req.setAttribute("status", "success");
        req.setAttribute("openid", openid);
        if (Ax.hasAx(trusted)) {
            req.setAttribute("ax", new Ax(trusted));
        }
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }

}
